import sys

from PySide6.QtCore import QSortFilterProxyModel
from PySide6.QtWidgets import QFrame, QApplication, QMessageBox, QHeaderView

from backend.db.database import SessionLocal
from backend.services.certificate_services import CertificateServices
from front.models.certificates_list_model import CertificatesListModel, CertificatesConsumption
from front.ui.ui_certificates_list import Ui_Frame
from front.ui_logic.creat_certificate import CreatCertificate


class CertificatesList(QFrame, Ui_Frame):
    def __init__(self, parent=None, ):
        super().__init__(parent)
        self.setupUi(self)
        self.addcertificatesButton.clicked.connect(self.creat_certificates)
        self.checkBox.toggled.connect(self.check_certificates)
    def set_table_view(self, data):
        data = [i.model_dump() for i in data]
        print(data)
        self.model = CertificatesListModel(data)
        self.proxy_model = QSortFilterProxyModel()
        self.proxy_model.setSourceModel(self.model)
        self.tableView.setModel(self.proxy_model)
        self.creat_tableview_button()
        self.tableView.setSortingEnabled(True)
        self.tableView.resizeColumnsToContents()
        header = self.tableView.horizontalHeader()
        header.setSectionResizeMode(QHeaderView.Stretch)

    def connect_consumption_button(self, callback_func):
        """按钮连接函数"""
        delegate = CertificatesConsumption()
        delegate.consume_callback = callback_func
        self.tableView.setItemDelegate(delegate)

    def creat_tableview_button(self):
        """创建按钮"""
        self.connect_consumption_button(self.clicked_consumption_button)

    def clicked_consumption_button(self, row: int):
        with SessionLocal() as session:
            certificate_service = CertificateServices(session)
            model = self.proxy_model
            if model is None:
                return
            member_id = model.index(row, 1).data()
            certificates = model.index(row, 0).data()
            print(f"为用户 {member_id} (ID: {certificates}) 消费存单")
            certificate_service.change_certificate(member_id, certificates)
            QMessageBox.information(self, "成功", "消费记录登记成功")
            self.search_certificates_information()

    def search_certificates_information(self, value=None):
        """搜索会员，更新tableview"""
        with SessionLocal() as session:
            print("执行了")
            certificate_service = CertificateServices(session)
            data = certificate_service.get_certificates(value)
            self.set_table_view(data)

    def creat_certificates(self):
        creat_certificate_creat = CreatCertificate(parent=self)
        creat_certificate_creat.remind.connect(self.search_certificates_information)
        creat_certificate_creat.show()

    def check_certificates(self):
        if self.checkBox.isChecked():
            with SessionLocal() as session:
                print("执行了")
                certificate_service = CertificateServices(session)
                data = certificate_service.get_not_consumed()
                self.set_table_view(data)
        else:
            self.search_certificates_information()


if __name__ == '__main__':
    app = QApplication([])
    window = CertificatesList()
    window.search_certificates_information()
    window.show()
    sys.exit(app.exec())
